﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH.Manager.BaseSystem
{
    public partial class MenuList : BasePage
    {
        string strMsg = "", MenuCode = "";
        ExcuteDao ExcuteDao = new ExcuteDao();

        protected void Page_Load(object sender, EventArgs e)
        {
            // 
            if (!IsPostBack)
            {
                lbtDel.OnClientClick = "return confirm('确认删除所选菜单吗');";
                ShowTree();

                if (Session["msg"] != null)
                {
                    ShowMsg(Session["msg"].ToString(), 0);
                    Session["msg"] = null;
                }

            }


        }

        //显示菜单树
        public void ShowTree()
        {
            TreeView_Menu.Nodes.Clear();
            TreeNode node_0 = new TreeNode();
            node_0.ImageUrl = "../Images/Small/calendar.gif";
            node_0.Text = "网站后台管理系统 <a herf='#'></a>";
            node_0.Value = "1";
            node_0.Text += "&nbsp;&nbsp;<a target='_self' href='MenuInsert.aspx?code=" + node_0.Value.ToString().Trim() + "'><font color='#0000FF'>增加子菜单</font></a>";
            node_0.SelectAction = TreeNodeSelectAction.Expand;
            node_0.Expanded = true;
            this.TreeView_Menu.Nodes.Add(node_0);

            this.ViewState["ds"] = ExcuteDao.GetQueryStrMenu();

            AddTree(node_0.Value, node_0);

        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="ParentID"></param>
        /// <param name="pNode"></param>
        public void AddTree(String ParentID, TreeNode pNode)
        {
            DataSet ds = (DataSet)this.ViewState["ds"];
            DataView dvTree = new DataView(ds.Tables[0]);
            dvTree.RowFilter = " Menu_Pcode = '" + ParentID + "'";
            int row = ExcuteDao.GetCountParentNode(ParentID);
            foreach (DataRowView Row in dvTree)
            {
                TreeNode Node = new TreeNode();


                Node.Value = Row["Menu_Code"].ToString().Trim();
                Node.Text = Row["Menu_Name"].ToString().Trim() + "<a herf='#'></a>";
                Node.Text += " <font style='font-size: 11px; display:none;' color='#808080'>[" + Node.Value.ToString().Trim() + "]</font> ";

                if (Row["Menu_FileName"].ToString().Trim().Length != 0)
                {
                    Node.Text += "<font style='font-size: 11px' color='#808080'>[" + Row["Menu_FileName"].ToString().Trim() + "]</font> ";
                }
                Node.Text += "<font style='font-size: 11px' color='#808080'>排序 [" + Row["Menu_DisplayOrder"].ToString().Trim() + "]</font>";
                if (Row["Menu_IsUse"].ToString().Trim() != "1")
                {
                    Node.Text += " <font color='#ff0000'>[停用]</font> ";
                }
                if (row > 0)
                {
                    Node.Text += "&nbsp;&nbsp;<a target='_self' href='MenuInsert.aspx?code=" + Node.Value.ToString().Trim() + "'><font color='#0000FF'>增加子菜单</font></a>";
                }

                Node.Text += "&nbsp;&nbsp;<a target='_self' href='MenuUpdate.aspx?code=" + Node.Value.ToString().Trim() + "'><font color='#0000FF'>编辑</font></a>";
                pNode.ChildNodes.Add(Node);
                Node.Expanded = true;
                if (Row["Menu_Ico"].ToString().Trim() == "")
                {
                    Node.ImageUrl = "../images/Small/notes.gif";
                }
                else
                {
                    Node.ImageUrl = "../images/Small/" + Row["Menu_Ico"].ToString().Trim();
                }
                if (pNode != null)
                {
                    MenuCode = "'" + Row["Menu_Code"].ToString().Trim() + "'";
                }

                AddTree(Row["Menu_Code"].ToString(), Node);    //再次递归


            }

        }

        /* 得到所选择的节点=========================================================
        * 调用方法 string MenuCodeAll = GetTreeCheck(TreeView_Menu.Nodes[0]);
        * 
        */
        string FunctionString = "";
        public string GetTreeCheck(TreeNode tn)
        {
            foreach (TreeNode treenode in tn.ChildNodes)
            {
                if (treenode.Checked)
                {
                    string va = treenode.Value;
                    string vb = treenode.Text;
                    FunctionString += ", " + va + "";
                }
                else
                {
                }

                GetTreeCheck(treenode);//调用自身函数循环遍历TreeView

            }
            return FunctionString;//返回FunctionString字符串
        }

        protected void lbtDel_Click(object sender, EventArgs e)
        {
            string MenuCodeAll = GetTreeCheck(TreeView_Menu.Nodes[0]);
            string[] mc = MenuCodeAll.Split(',');

            if (MenuCodeAll == "")
            {
                //Session["Msg"] = "<script>alert('未选择菜单，删除失败！');</script>";
                ShowMsg("未选择菜单，删除失败！", 1);
                return;
            }
            else
            {
                for (int i = 0; i < mc.Length; i++)
                {
                    ExcuteDao.DelMenu(mc[i].ToString().Trim());
                }
                Session["msg"] = "执行成功！";
                Response.Write("<script>top.window.location='../Welcome.aspx?local=BaseSystem/MenuList.aspx';</script>");

            }

            ShowTree();
            //Session["Msg"] = "<script>alert('操作已提交。某些菜单若被角色使用，则不能被删除！');</script>";
            return;
        }
    }
}